package com.franklin.ideaplugin.api.utils;

import com.franklin.ideaplugin.easytesting.icon.PluginIcons;
import com.intellij.openapi.ui.Messages;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * @author Ye Junhui
 * @since 2023/5/12
 */
public class SwingUtils {

    public static GridLayoutManager createLayoutManager(int rowCount, int columnCount){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(rowCount, columnCount);
        gridLayoutManager.setVGap(1);
        gridLayoutManager.setHGap(1);
        return gridLayoutManager;
    }

    /**
     * 确认消息弹框
     * @param title
     * @param message
     * @return
     */
    public static boolean showConfirmDialog(String title,String message){
        return Messages.showYesNoDialog(title,message, PluginIcons.PLUGIN_ICON) == Messages.YES;
    }

    public static void addCell(JComponent table, JComponent component, int row, int column, Dimension size){
        table.add(
                component,
                new GridConstraints(row, column, 1, 1,
                        GridConstraints.ANCHOR_WEST, GridConstraints.FILL_BOTH,
                        GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED,
                        size, size, new Dimension(Integer.MAX_VALUE,Integer.MAX_VALUE))
        );
    }

    /**
     * 清空actionListener
     * @param button
     */
    public static void clearActionListener(JButton button){
        for (ActionListener actionListener : button.getActionListeners()) {
            button.removeActionListener(actionListener);
        }
    }
}
